package com.qk.controller;


import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;





    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("id:{}",id);
    roleService.deleteById(id);
    return Result.success();
    }

    @PostMapping("/roles")
    public Result addRole(@RequestBody Role role){
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        log.info("role:{}",role);
    roleService.addRole(role);
    return Result.success();
    }

    @GetMapping("/roles/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("id:{}",id);
    Role role = roleService.getById(id);
    return Result.success(role);
    }

    @PutMapping("/roles")
    public Result update(@RequestBody Role role){
        log.info("role:{}", role);

    roleService.update(role);
    return Result.success();
    }

    @GetMapping("/roles/list")
    public Result listAll(){
        log.info("status:{}");
        return Result.success(roleService.listAll(null));

    }


    @GetMapping("/roles")
    public Result listRoles(@RequestParam String name ,String label,
                            @RequestParam(defaultValue = "1") Integer page ,
                            @RequestParam(defaultValue = "10") Integer pageSize)
        {
        log.info("name:{},label:{},page:{},pageSize:{}",name,label,page,pageSize);
        PageResult<Role> pageResult = roleService.listRoles(name,label,page,pageSize);
        return Result.success (pageResult);
        }
    }






















